CAD Keys

CAD using the Keys

© 2004, Gordon Sweet

author contact:

gordon@gsweet.fsnet.co.uk

Home

Random Access Files

Liberty Simple Help2

Scrolling Background

Blood Presure Sim

Cellular Automata

Making Poker Game

Desktop Shortcuts2

Beginners Programming

Demos

Newsletter help

Index


CAD using the Keys

To many, another CAD program might seem rather pointless, with the abundance of so many others available, even if this version is intended to provide greater precision when drawing through the use of the keys. However it might also provide an example to beginners to LB in demonstrating the use of the keys to control a program instead of the mouse.

To understanding how to persuade the program to recognize the activation of a key as well as the mouse, look at the lines in [Loop] and read the references in the LB Help file under View Graphics Windows Commands. The other points to study are the two uses of Inkey$ to determine the string value, and the variable keyV which differs from the straight ASCII value of Inkey$ only when certain keys such as the cursor keys are pressed. This is needed of course to check such as when a cursor key is used to move the drawing position.

Much of the remaining code is fairly routine, and in trying to understand this beginners should constantly refer to the excellent Help section of LB. To save downloading space I have not included the ping.wav and dong wav files, LB defaults to another sound when it cannot find a WAV file. You can experiment with other WAV files by renaming the ping.wav and dong.wav if desired.

In the original version, of this program, a problem arouse while using the 16 bit version of LB 2.02, when holding down a cursor key. This appears to have been completely cured with the 32 bit versions 3.03 and 4. Another problem arose in losing part of the display, after using the filedialog or colordialog. This appears to have been solved by the judicious use of FLUSH and REDRAW such as in the [save] and [load] subroutines.

[Editors Note: some of the functions require a pretty fast PC to keep up with the keyboard buffer - be careful not to overshoot in move mode due to the slower movement of drawing the circle cursor. This would be an interest program to carry to the next level as a nice 2D cad tool.]

Gordon Sweet



'  Precision CAD using mainly the KEYS
    nomainwin
    ux = 1 : uy = 1
    if DisplayWidth > 1000 then ux = 120 : uy = 90
    UpperLeftX = ux : UpperLeftY = uy
    WindowWidth = 800 : WindowHeight = 580
menu #1, &OPTIONS, &Help, [help], &Load, [load], &Save, [save], _
&Clear, [clear], &Text, [text], |, &Quit, [quit]
menu #1, &DRAWING, &Draw, [draw], &Move, [move], &Erase, [erase], &Colour, _
[lcol], &Width, [width],&Box, [box], &Circle, [circle], &Fill, [fill]
    open "Keys CAD ****** Drawing Mode *******" for graphics_nsb as #1
    #1 "cls; trapclose [quit]; when rightButtonDown [point]"
    #1 "goto 10 10; down"
    x=10 : y=10 : draw=2 : fix=0 : col=0 : col$="black"
    playwave "ping.wav" :  goto [help]

[Loop]
    #1 "setfocus; when characterInput [fetch]"
    wait

[fetch]
    key$ = Inkey$
    keyV = asc(right$(key$, 1))
rem    #1 "\";x;" ";y:#1 t$  'check xy points
    if keyV=38 and y>3 then y=y-1
    if keyV=40 and y<550 then y=y+1
    if keyV=37 and x>3 then x=x-1
    if keyV=39 and x<785 then x=x+1
    if keyV=33 and y>33 then y=y-10
    if keyV=34 and y<540 then y=y+10
    if keyV=36 and x>33 then x=x-10
    if keyV=35 and x<780 then x=x+10
    if key$="d" or key$="D" then goto [draw]
    if key$="m" or key$="M" then goto [move]
    if key$="e" or key$="E" then goto [erase]
    if draw < 2 then
    #1 "color red; place ";x;" ";y
    #1 "rule xor; circlefilled 2"
    tim=time$("ms"):while time$("ms")<tim+100:wend
    #1 "place ";x;" ";y
    #1 "circlefilled 2"
    if draw = 0 then #1 "rule over; color white; boxfilled ";x+2;" ";y+2
    if draw = 1 then #1 "rule OVER; goto ";x;" ";y
    end if
    if draw = 2 then
    #1 "color ";col$
    #1 "goto ";x;" ";y : fix=0
    end if
    goto [Loop]

[draw] draw = 2 : goto [Loop]
[move] draw = 1 : goto [Loop]
[erase] draw = 0 : goto [Loop]

[lcol] gosub [cols] : goto [Loop]

[fill] gosub [cols]
    confirm "OK to fill and clear screen?"; q$
    if q$="yes" then #1 "fill ";col$
    goto [Loop]

[point] x=MouseX : y=MouseY
    fix=1 : playwave "ping.wav"
    #1 "place ";x;" ";y : goto [Loop]

[clear]
    confirm "Clear Screen?";q$
    if q$="yes" then #1 "goto 10 10; cls"
    goto [Loop]

[width] I$=""
    prompt "Enter Line Width 1/10"; i$
    if i$="" then [Loop]
    width=VAL(i$)
    if width<1 or width>10 then beep : goto [Loop]
    #1 "size ";i$
    goto [Loop]

[size]
    #1 "flush"
    i$="" : prompt "Enter Radius 2/300"; i$
    if i$="" then [Loop]
    s=VAL(i$)
    if s<2 or s>300 then goto [size]
    confirm "Fill with colour?";q$
    #1 "redraw" : return

[circle]
    gosub [size] : op$="circle "
    if q$="yes" then
        op$="circlefilled "
        gosub [cols]
        #1 "backcolor ";col$
    end if
    #1 op$;" ";i$ : goto [Loop]

[box] gosub [size]
    x2=x+s:y2=y+s : op$="box "
    if q$="yes" then
        op$="boxfilled "
        gosub [cols]
        #1 "backcolor ";col$
    end if
    #1 op$;x2;" ";y2 : goto [Loop]

[text] t$=""
    prompt "Right Click to set position. Enter Text "; t$
    if t$="" then [Loop]
    #1 "\";t$ : goto [Loop]

[load]
    #1 "flush"
    filedialog "Select a BMP file", "*.bmp", fil$ 
    if fil$="" then playwave "dong.wav":goto [Loop]
    loadbmp "title", fil$ 
    #1 "redraw; drawbmp title ";x;" ";y
    goto [Loop]

[save]
    if l>600 or w>800 then beep : goto [Loop]
    #1 "flush" : gosub [sbox]
    filedialog "Enter a BMP file", "*.bmp", fil$ 
    if fil$="" then playwave "dong.wav" : goto [Loop]
    #1 "redraw"
    #1 "getbmp drawing 1 1 ";w;" ";l
    bmpsave "drawing", fil$
    goto [Loop]

[help]
    #1 "flush"
    open "Help Screen" for graphics_nsb as #2
    #2 "cls; trapclose [cont]; color darkblue; font arial 24 bold; place 300 120;"
    #2 "\WKEYSCAD": #2 "font arial 10 bold; place 280 150"
    #2 "\by Gordon Sweet - Ver. 3.4 January 2003"
    #2 "color black; font arial 16 bold; place 70 200"
    #2 "\Use either the cursor keys to draw/erase or PdUp, PgDn, Home, End"
    #2 "\Press PrintScreen+Alt to copy to the Clipboard and Paste to a DTP."
    #2 "\Use the Menus to select Drawing, and right click to Move position."
    #2 "\You can also press D to Draw or E to erase or M to Move with keys."
    #2 "flush" : notice "OK to Continue?"
[cont] close #2: goto [Loop]

[cols]
    colordialog "white", col$ 
    if col$="" then
        notice "Nothing Delected!"
        goto [cols]
    end if
    return

[sbox]
    if bx=1 then close #3
    UpperLeftX = 10+ux : UpperLeftY = 60+uy
    WindowWidth = 160: WindowHeight = 120
    statictext #3, "Width", 30, 10, 50, 20
    statictext #3, "Height", 100, 10, 40, 20
    textbox #3.tbox1, 30, 30, 30, 24
    textbox #3.tbox2, 100, 30, 30, 24
    button #3.default, "Accept", [pass2], UL, 60, 60, 42, 25
    open "ACCEPT or Enter" for dialog as #3
    #3 "trapclose [nopass2]"
    print #3.tbox1, "!setfocus"
[nopass2]
    playwave "ping.wav" : bx=1
    mainH=hWnd(#3) : gosub [boxhold]
    wait

[pass2]
    print #3.tbox1, "!contents? A$" : w=val(A$)
    print #3.tbox2, "!contents? B$" : l=val(B$)
    if w<10 or l<10 then playwave "dong.wav" : goto [nopass2]
    bx=0 : close #3
    return

[quit]
    if bx=1 then close #3
    if col=1 then close #9
    confirm "OK to Quit?";q$
    if q$="no" then goto [Loop]
    close #1 : end

[boxhold]
    open "user32" for dll as #user ' prevents losing box
        toTop=(-1 or 0)
        flags=_SWP_NOMOVE or _SWP_NOSIZE
    calldll #user,"SetWindowPos",mainH as ushort,toTop as short,_
    0 as short,0 as short,0 as short,0 as short,flags as ushort,_
    result as void
    close #user : return

[dats]
    data "255 255 255","255 150 150","255 100 160"
    data "255 1 1 ","180 50 1","255 150 150"
    data "255 1 200","150 1 80","255 120 1"
    data "255 255 1","200 200 150","1 255 1"
    data "1 120 1","50 255 255","1 1 255","20 100 200"
    data "1 1 180","1 200 255","100 100 100","0 0 0"


Home

Random Access Files

Liberty Simple Help2

Scrolling Background

Blood Presure Sim

Cellular Automata

Making Poker Game

Desktop Shortcuts2

Beginners Programming

Demos

Newsletter help

Index